/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Client.domain;

import Client.persistence.PictureLoader;
import java.awt.Color;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;

/**
 *
 * @author Ripper
 */
public class ImageProcessor 
{
    private PictureLoader picLoad;
    
    private Color[] replaceAbleColors = {
        new Color(237,28,36),
        new Color(34,177,76),
        new Color(0,162,232),
        new Color(255,242,0)
    };
    
    public static BufferedImage scaleToFitSize(BufferedImage bi, int size)
    {
        double xscl = (double)size / (double)bi.getWidth();
        double yscl = (double)size / (double)bi.getHeight();
        
        BufferedImage scaledImage = new BufferedImage((int)(bi.getWidth()*xscl), 
                (int)(bi.getHeight()*yscl), BufferedImage.TYPE_INT_ARGB);
        AffineTransform at = new AffineTransform();
        
        
        at.scale(xscl, yscl);
        AffineTransformOp scaleOp = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
        return scaleOp.filter(bi, scaledImage);
    }
    
    public ImageProcessor(PictureLoader pictureloader)
    {
        picLoad = pictureloader;
    }
    
    public BufferedImage surroundImageWithBorder(BufferedImage original, Color border)
    {
        BufferedImage bi = new BufferedImage(original.getWidth(), original.getHeight(), BufferedImage.TYPE_INT_ARGB);
        
        /*for(int a = 0; a < original.getWidth(); a++)
        {
            for(int b = 0; b < original.getHeight(); b++)
            {
                bi.setRGB(a, b, (new Color(255,0,255)).getRGB());
            }
        }*/
        
        Color transparant = new Color(255,255,255,0);
        //TODO!!! still buggy
        for(int a = 0; a < original.getWidth(); a++)
        {
            for(int b = 0; b < original.getHeight(); b++)
            {
                Color colAtlocation = getColorAt(a,b,original);
                
                if(isColorTransparent(colAtlocation))
                {
                    if((!isColorTransparent(getColorAt(a - 1, b, original)) ||
                    !isColorTransparent(getColorAt(a + 1, b, original)) ||
                    !isColorTransparent(getColorAt(a, b + 1, original)) ||
                    !isColorTransparent(getColorAt(a, b - 1, original))))
                    {
                        bi.setRGB(a, b, border.getRGB());
                    }
                    else
                    {
                        bi.setRGB(a,b,transparant.getRGB());
                    }
                }
                else
                {
                    bi.setRGB(a, b, colAtlocation.getRGB());
                }
            }
        }
        
        return bi;
    }
    public Color getColorAt(int x, int y, BufferedImage bi)
    {
        try
        {
            return new Color(bi.getRGB(x, y));
        }
        catch(Exception ex){
            return null;
        }
    }
    public boolean isColorTransparent(Color c)
    {
        if(c==null)
        {
            return true;
        }
        
        return c.getRed() == c.getGreen() 
            && c.getGreen() == c.getBlue()
            && c.getBlue() == 255;
    }
    
    public BufferedImage createFancyImage(String id, Color[] colors)
    {
        BufferedImage greyValues = picLoad.getGreyValues(id);
        BufferedImage colorValues = picLoad.getColourValues(id);
        
        BufferedImage fancyImage = new BufferedImage(greyValues.getWidth(), greyValues.getHeight(), BufferedImage.TYPE_INT_ARGB);
        
        for(int x = 0; x < greyValues.getWidth(); x++)
        {
            for(int y = 0; y < greyValues.getHeight();y++)
            {
                Color greyColor = new Color(greyValues.getRGB(x, y));
                Color colorColor = new Color(colorValues.getRGB(x, y));
                
                if(greyColor.getRed() == 0 && greyColor.getGreen() == 0 && greyColor.getBlue() == 0)
                {
                    Color fancyColor = new Color(255,255,255,0);
                    fancyImage.setRGB(x, y, fancyColor.getRGB());
                }
                else
                {
                    double f = (double)greyColor.getGreen() / (double)255;
                    
                    boolean matchFound = false;
                    int a = 0;
                    for(Color c: replaceAbleColors)
                    {
                        if(doColorsMatch(c, colorColor))
                        {
                            Color col = colors[a];
                            
                            int r = col.getRed();
                            int g = col.getGreen();
                            int b = col.getBlue();
                            
                            r = (int)((double)r * f);
                            g = (int)((double)g * f);
                            b = (int)((double)b * f);
                            
                            matchFound = true;
                            
                            Color clr = new Color(r,g,b,255);
                            fancyImage.setRGB(x, y, clr.getRGB());
                        }
                        a++;
                    }
                    
                    if(!matchFound)
                    {
                        Color fancyColor = new Color(255,255,255,0);
                        fancyImage.setRGB(x, y, fancyColor.getRGB());
                    }
                }
            }
        }
        
        return fancyImage;
    }
    
    private boolean doColorsMatch(Color a, Color b)
    {
        return (a.getRed() == b.getRed() &&
                a.getGreen() == b.getGreen() &&
                a.getBlue() == b.getBlue());
    }

    public BufferedImage mirrorHorizontally(BufferedImage img) 
    {
        BufferedImage mirror = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
        
        Color transparent = new Color(255,255,255,0);
        for(int x = 0; x < img.getWidth(); x++)
        {
            for(int y = 0; y < img.getHeight(); y++)
            {
                Color c = new Color(img.getRGB(x, y));
                if(isColorTransparent(c))
                {
                    mirror.setRGB(img.getWidth() - 1 - x, y, transparent.getRGB());
                }
                else
                {
                    mirror.setRGB(img.getWidth() - 1 - x, y, c.getRGB());
                }
            }
        }
        
        return mirror;
    }
    
    public BufferedImage makeWhitesTransparent(BufferedImage img)
    {
        BufferedImage copy = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
        
        Color transparent = new Color(255,255,255,0);
        for(int x = 0; x < img.getWidth(); x++)
        {
            for(int y = 0; y < img.getHeight(); y++)
            {
                Color colAtLoc = getColorAt(x, y, img);
                
                if(colAtLoc.getRed() == 255 && colAtLoc.getGreen() == 255 && colAtLoc.getBlue() == 255)
                {
                    copy.setRGB(x, y, transparent.getRGB());
                }
                else
                {
                    copy.setRGB(x, y, colAtLoc.getRGB());
                }
            }
        }
        
        return copy;
    }
}
